<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="inet.db.tennis.TennisPhase"%>
<%@page import="inet.db.tennis.TennisSeason"%>
<%@page import="inet.db.tennis.buffer.TennisBuffer"%>
<%@page import="inet.db.tennis.TennisRound"%>
<%@page import="inet.util.DateTime"%>
<%@page import="inet.util.DateProc"%>
<%@page import="inet.util.StringTool"%>
<%@page import="inet.db.tennis.TennisMatch"%>
<%@page import="inet.db.tennis.TennisPlayer"%>
<jsp:useBean id="tennisPhaseDAO" class="inet.db.tennis.TennisPhaseDAO" scope="session"/>
<jsp:useBean id="tennisRoundDAO" class="inet.db.tennis.TennisRoundDAO" scope="session"/>
<jsp:useBean id="tennisMatchDAO" class="inet.db.tennis.TennisMatchDAO" scope="session"/>
<jsp:useBean id="tennisSeasonDAO" class="inet.db.tennis.TennisSeasonDAO" scope="session"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Sửa trận đấu</title>
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/reset.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/text.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/grid.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/layout.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/nav.css" media="screen" />
    <!--[if IE 6]><link rel="stylesheet" type="text/css" href="css/ie6.css" media="screen" /><![endif]-->
    <!--[if IE 7]><link rel="stylesheet" type="text/css" href="css/ie.css" media="screen" /><![endif]-->
    <link href="<%= request.getContextPath() %>/admin/tennis/css/fancy-button/fancy-button.css" rel="stylesheet" type="text/css" />
    <link href="<%= request.getContextPath() %>/admin/tennis/css/style.css" rel="stylesheet" type="text/css" />
    <!--Jquery UI CSS-->
    <link href="<%= request.getContextPath() %>/admin/tennis/css/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />
    <!-- BEGIN: load jquery -->
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.core.min.js"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.widget.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.accordion.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.effects.core.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.effects.slide.min.js" type="text/javascript"></script>
    <!-- END: load jquery -->
    
    <!--Fancy Button-->
    <script src="<%= request.getContextPath() %>/admin/tennis/js/fancy-button/fancy-button.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/setup.js" type="text/javascript"></script>
    <script type="text/javascript">
    	function f_edit_match() {
    		if(document.formEditMatch.slbSeason.value=='0'){
				document.getElementById("lblSeason").innerHTML = 'Chọn mùa giải';
				document.formEditMatch.slbSeason.focus();
				return false;
			}else{
				document.getElementById("lblSeason").innerHTML = '';
			}
    		if(document.formEditMatch.slbPhase.value=='0'){
				document.getElementById("lblPhase").innerHTML = 'Chọn giai đoạn';
				document.formEditMatch.slbPhase.focus();
				return false;
			}else{
				document.getElementById("lblPhase").innerHTML = '';
			}
    		if(document.formEditMatch.slbRound.value=='0'){
				document.getElementById("lblRound").innerHTML = 'Chọn vòng đấu';
				document.formEditMatch.slbRound.focus();
				return false;
			}else{
				document.getElementById("lblRound").innerHTML = '';
			}
    		
    		document.formEditMatch.action.value = "UPDATE";
    		document.formEditMatch.submit();
    	}
    	function disableEnterKey(e)
		{
		     var key;
		     if(window.event)
		          key = window.event.keyCode;//IE
		     else
		          key = e.which;//firefox
		     if(key == 13)
		        f_add_match();
		}
    </script>
</head>
<body>
    <div class="container_12">
        <%@ include file="../include/header.jsp" %>
        <% 
        	// set Character Encoding before any getParameter() methods.
		    request.setCharacterEncoding("UTF-8");
		    response.setCharacterEncoding("UTF-8");
		    
		    String sId = request.getParameter("id");
		    if(sId == null || !StringTool.isNumberic(sId)) {
		    	sId = "0";
		    }
		    BigDecimal matchId = new BigDecimal(sId);
		    
		    String sAction = request.getParameter("action");
		    String sChampionCode = request.getParameter("championCode");
		    String sSeason = request.getParameter("slbSeason");
		    BigDecimal seasonId = null;
		    if (sSeason != null) seasonId = new BigDecimal(sSeason);
		    
		    String sPhaseId = request.getParameter("slbPhase");
		    BigDecimal phaseId = null;
		    if (sPhaseId != null) phaseId = new BigDecimal(sPhaseId);
		    
		    String sRound = request.getParameter("slbRound");
		    String sTotalSet = request.getParameter("slbTotalSet");
		    String sSTT = request.getParameter("txtSTT");
		    
		    String sPlayer1 = "";
			String sPlayer2 = "";
			sPlayer1 = request.getParameter("slbPlayer1");
			sPlayer2 = request.getParameter("slbPlayer2");
			String day = "";
			String month = "";
			String year = "";
			String hour = "";
			String minute = "";
			
			day = request.getParameter("day");
			month = request.getParameter("month");
			year = request.getParameter("year");
			hour = request.getParameter("hour");
			minute = request.getParameter("minute");
		    
		    String sMessageErr = "";
		    String sMessageOk = "";
		    String sMessageWarning = "";
		    
		    if(sAction != null && "UPDATE".equals(sAction)) {
		    	if (seasonId == null || seasonId == BigDecimal.ZERO) {
		    		sMessageWarning = "Bạn phải chọn mùa giải";
		    	} else if (phaseId == null || phaseId == BigDecimal.ZERO ) {
		    		sMessageWarning = "Bạn phải chọn giai đoạn";
		    	} else if (sRound == null || "0".equals(sRound) ) {
		    		sMessageWarning = "Bạn phải chọn vòng đấu";
		    	} else if (sSTT == null || !StringTool.isNumberic(sSTT) ) {
		    		sMessageWarning = "Số thứ tự trận đấu của vòng phải là số";
		    	} else {
		    	    int maxRoundNo = tennisRoundDAO.getMaxNoOfRound(new BigDecimal(sPhaseId));
		    	    DateTime matchDate = new DateTime(DateProc.createTimestamp());
					matchDate.setDay(Integer.parseInt(day));
					matchDate.setMonth(Integer.parseInt(month));
					matchDate.setYear(Integer.parseInt(year));
					matchDate.setHour(Integer.parseInt(hour));
					matchDate.setMinute(Integer.parseInt(minute));
					matchDate.setSecond(0);
					matchDate.refresh();
		    	    
					int sttMatch = Integer.parseInt(sSTT);
		    		boolean isOk = tennisMatchDAO.updateRow(matchId, sRound, matchDate.getTimestamp(), seasonId, phaseId, sttMatch, Integer.parseInt(sTotalSet));
		    		if(isOk) {
		    			sMessageOk = "Sửa trận đấu thành công";
		    		} else {
		    			sMessageErr = "Xảy ra lỗi khi sửa trận đấu ";
		    		}
		    	}
		    }
		    
		    TennisMatch match = tennisMatchDAO.getRow(matchId);
        %>
        <div class="grid_5">
            <div class="box round first fullpage">
                <h2>Sửa trận đấu</h2>
                <div class="block ">
                	<% 
                		if(match != null) {
                			DateTime matchTime = new DateTime(match.getMatchDate()); 
                			if(seasonId == null) seasonId = match.getSeasonId();
                			if(phaseId == null) phaseId = match.getPhaseId();

                			TennisPlayer player1 = (TennisPlayer) TennisBuffer.getPlayerFromId(match.getFirstPlayerId());
							TennisPlayer player2 = (TennisPlayer) TennisBuffer.getPlayerFromId(match.getSecondPlayerId());
                	%>
                    <form name="formEditMatch" onKeyPress="return disableEnterKey(event)" method="post">
                    	<input type="hidden" name="action"/>
                    	<input type="hidden" name="phaseId" value="<%= sPhaseId %>"/>
                    	<input type="hidden" name="id" value="<%= sId %>"/>
                    	<% if( !"".equals(sMessageWarning) ) { %>
                    	<div class="message warning">
                            <h5>Cảnh báo !</h5>
                            <p>
                                <%= sMessageWarning %>
                            </p>
                        </div>
                        <% } %>
                    	<% if( !"".equals(sMessageOk) ) { %>
                    	<div class="message success">
                            <h5>Success!</h5>
                            <p>
                                <%= sMessageOk %>
                            </p>
                        </div>
                        <% } %>
                    	<% if( !"".equals(sMessageErr) ) { %>
                    	<div class="message error">
                            <h5>Error!</h5>
                            <p>
                                <%= sMessageErr %>
                            </p>
                        </div>
                        <% } %>
	                    <table class="form">
	                        <tr class="eventt">
	                            <td class="col1">
	                                <label>Trận đấu</label>
	                            </td>
	                            <td class="col2">
	                                <strong><%= player1 != null ? player1.getName():"" %> vs <%= player2 != null ? player2.getName():"" %></strong> 
	                            </td>
	                        </tr>
	                        <tr class="eventt">
	                            <td class="col1">
	                                <label>Loại trận đấu</label>
	                            </td>
	                            <td class="col2">
	                                <strong><%= match.getIsMatchInv() == 1 ? "<span style='color:#990000;'>Trận điều kiện</span>":"Trận bình thường" %></strong> 
	                            </td>
	                        </tr>
	                        <tr class="eventt">
	                            <td class="col1">
	                                <label>Giải đấu</label>
	                            </td>
	                            <td class="col2">
	                                <strong><%= match.getChampionName() %></strong>
	                            </td>
	                        </tr>
	                        <tr class="eventt">
	                            <td>
	                                <label>Mùa giải</label>
	                            </td>
	                            <td>
	                                <select name="slbSeason" size="1" onchange="document.formEditMatch.submit();">
	                                	<option value="0"> - Chọn mùa giải - </option>
	                                	<%
	                                		Vector<TennisSeason> vSeasons = tennisSeasonDAO.findAll(match.getChampionCode());
			                                if ((vSeasons != null) && (vSeasons.size() > 0)) {
			                                	TennisSeason season = null;
			                                    String selected = "";
			                                    for (Iterator it = vSeasons.iterator(); it.hasNext();) {
			                                    	season = (TennisSeason) it.next();
			                                        if (season == null) continue;
			                                        if ((season.getId()).equals(seasonId)) selected = "selected";
			                                        else selected = "";
			                            %>
			                           	 		<option value="<%=season.getId()%>" <%=selected%>><%=season.getSeasonName()%></option>
			                            <%
			                            		}
			                                }
			                            %>
	                                </select>
	                                <span id="lblSeason" class="error"></span>
	                            </td>
	                        </tr>
	                        <tr class="eventt">
	                            <td>
	                                <label>Giai đoạn</label>
	                            </td>
	                            <td>
	                                <select name="slbPhase" size="1" onchange="document.formEditMatch.submit();">
	                                	<option value="0"> - Chọn giai đoạn - </option>
	                                	<%
	                                		Vector<TennisPhase> vPhase = tennisPhaseDAO.findAll(match.getChampionCode(), seasonId);
			                                if ((vPhase != null) && (vPhase.size() > 0)) {
			                                	TennisPhase phase = null;
			                                    String selected = "";
			                                    for (Iterator it = vPhase.iterator(); it.hasNext();) {
			                                    	phase = (TennisPhase) it.next();
			                                        if (phase == null) continue;
			                                        if ((phase.getId()).equals(phaseId)) selected = "selected";
			                                        else selected = "";
			                            %>
			                           	 		<option value="<%=phase.getId()%>" <%=selected%>><%=phase.getPhaseName()%></option>
			                            <%
			                            		}
			                                }
			                            %>
	                                </select>
	                                <span id="lblPhase" class="error"></span>
	                            </td>
	                        </tr>
	                        <tr class="eventt">
	                            <td>
	                                <label>Vòng đấu</label>
	                            </td>
	                            <td>
	                                <select name="slbRound" size="1">
	                                	<option value="0"> - Chọn vòng đấu - </option>
	                                	<%
	                                		Vector<TennisRound> vRound = tennisRoundDAO.findAll(phaseId);
			                                if ((vRound != null) && (vRound.size() > 0)) {
			                                	TennisRound round = null;
			                                    String selected = "";
			                                    for (Iterator it = vRound.iterator(); it.hasNext();) {
			                                    	round = (TennisRound) it.next();
			                                        if (round == null) continue;
			                                        if (round.getRoundCode().equals(match.getRound())) selected = "selected";
			                                        else selected = "";
			                            %>
			                           	 		<option value="<%=round.getRoundCode()%>" <%=selected%>><%=round.getRoundCode()%></option>
			                            <%
			                            		}
			                                }
			                            %>
	                                </select>
	                                <span id="lblRound" class="error"></span>
	                            </td>
	                        </tr>
	                        <tr class="eventt">
	                        	<td><label>Thời gian</label></td>
	                        	<td>
	                        		Ngày
		                            <select name="day" size="1">
		                            <%
		                                 for (int i = 1; i <= 31; i++) {
		                                     out.println("<option "+ (i == matchTime.getDay() ? "selected":"") +" value='" + i + "'>" + i + "</option>");
		                                 }
		                            %>
		                            </select>
		
		                            Tháng
		                            <select name="month" size="1">
		                            <%
		                                 for (int i = 1; i <= 12; i++) {
		                                     out.println("<option "+ (i == matchTime.getMonth() ? "selected":"") +" value='" + i + "'>" + i + "</option>");
		                                 }
		                            %>
		                            </select>
		
		                            Năm
		                            <select name="year" size="1" id="cYear">
		                            <%
		                                 for (int i = 2012; i <= 2018; i++) {
		                                     out.println("<option "+ (i == matchTime.getYear() ? "selected":"") +" value='" + i + "'>" + i + "</option>");
		                                 }
		                            %>
		                            </select>
		                            <br />
		                            Giờ &nbsp;
		                            <select name="hour" size="1">
		                            <%
		                                 for (int i = 0; i <= 23; i++) {
		                                     out.println("<option "+ (i == matchTime.getHour() ? "selected":"") +" value='" + i + "'>" + i + "</option>");
		                                 }
		                            %>
		
		                            </select>
		                            &nbsp;Phút&nbsp;&nbsp;
		                            <select name="minute" size="1">
		                            <%
		                                 for (int i = 0; i <= 59; i++) {
		                                     out.println("<option "+ (i == matchTime.getMinute() ? "selected":"") +" value='" + i + "'>" + i + "</option>");
		                                 }
		                            %>
		
		                            </select>
	                        	</td>
	                        </tr>
	                        <tr class="eventt">
	                            <td>
	                                <label>Set đấu</label>
	                            </td>
	                            <td>
	                                <select name="slbTotalSet" size="1">
	                                	<option <%= match.getTotalSet() == 3 ? "selected":"" %> value="3"> - 3 SET - </option>
	                                	<option <%= match.getTotalSet() == 5 ? "selected":"" %> value="5"> - 5 SET - </option>
	                                </select>
	                                <span id="lblRound" class="error"></span>
	                            </td>
	                        </tr>
	                        <tr class="eventt">
	                            <td>
	                                <label>STT </label>
	                            </td>
	                            <td>
	                                <input type="text" name="txtSTT" value="<%= match.getStt() %>"/>
	                                <span id="lblSTT" class="error"></span>
	                            </td>
	                        </tr>
	                        <tr class="eventt">
	                        	<td>
	                                <label>&nbsp;</label>
	                            </td>
	                            <td>
	                            	<button type="button" onclick="f_edit_match();" class="btn btn-grey">Cập nhật</button>
	                            	<button type="button" onclick="window.location.href='list-match.jsp';" class="btn btn-grey">Quay lại</button>
	                            </td>
	                        </tr>
	                    </table>
                    </form>
                    <% } else { %>
                    <div class="message warning">
                        <h5>Cảnh báo !</h5>
                        <p>
                            Không tồn tại trận đấu có Id = <%= sId %>
                        </p>
                    </div>
                    <% } %>
                </div>
            </div>
        </div>
        <div class="clear">
        </div>
    </div>
    <%@ include file="../include/footer.jsp" %>
</body>
</html>